package org.dcarew.samplemodel.views;

import java.util.Collection;

import org.dcarew.model.core.IModelClass;
import org.dcarew.model.core.IModelProperty;
import org.dcarew.model.core.ModelObject;
import org.dcarew.samplemodel.model.Book;
import org.dcarew.samplemodel.model.BookCollection;
import org.dcarew.samplemodel.model.Magazine;
import org.eclipse.jface.viewers.ITreeContentProvider;
import org.eclipse.jface.viewers.LabelProvider;
import org.eclipse.jface.viewers.TreeViewer;
import org.eclipse.jface.viewers.Viewer;
import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.ui.part.ViewPart;

/**
 * 
 * 
 * @author dcarew
 */
public class SampleModelView
	extends ViewPart
{
	private TreeViewer treeViewer;
	
	
	public SampleModelView()
	{
		
	}
	
	public void createPartControl(Composite parent)
	{
		treeViewer = new TreeViewer(parent, SWT.NONE);
		treeViewer.setLabelProvider(new ModelLabelProvider());
		treeViewer.setContentProvider(new ModelTreeContentProvider());
		treeViewer.setInput(createSampleModel());
	}
	
	public void setFocus()
	{
		//treeViewer.setFocus();
	}
	
	private static BookCollection createSampleModel()
	{
		BookCollection books = new BookCollection();
		
		books.getItems().add(new Book("Alice in Wonderland", "ABC Publishers"));
		books.getItems().add(new Book("Sky of Blades", "Ace"));
		books.getItems().add(new Magazine("National Geographic"));
		
		return books;
	}

}

class ModelTreeContentProvider
	implements ITreeContentProvider
{

	public Object[] getChildren(Object parentElement)
	{
		if (parentElement instanceof ModelObject)
		{
			ModelObject modelObject = (ModelObject)parentElement;
			
			IModelClass modelClass = modelObject.getModelClass();
			
			IModelProperty[] properties = modelClass.getProperties();
			
			Object[] children = new Object[properties.length];
			
			for (int i = 0; i < properties.length; i++)
			{
				children[i] = modelObject.getPropertyValue(properties[i]);
			}
			
			return children;
		}
		else if (parentElement instanceof Collection)
		{
			return ((Collection)parentElement).toArray();
		}
		else
		{
			return new Object[0];
		}
	}

	public Object getParent(Object element)
	{
		return null;
	}

	public boolean hasChildren(Object element)
	{
		return getChildren(element).length > 0;
	}

	public Object[] getElements(Object inputElement)
	{
		return getChildren(inputElement);
	}

	public void dispose()
	{
		
	}

	public void inputChanged(Viewer viewer, Object oldInput, Object newInput)
	{
		
	}
}

class ModelLabelProvider
	extends LabelProvider
{

	public Image getImage(Object element)
	{
		if (element instanceof ModelObject)
		{
			return ((ModelObject)element).getModelClass().getIcon();
		}
		else
		{
			return null;
		}
	}

	public String getText(Object element)
	{
		if (element instanceof ModelObject)
		{
			return ((ModelObject)element).getModelClass().getName() + " " + element.toString();
		}
		else
		{
			return element == null ? "" : element.toString();
		}
	}
	
}
